package com.yoda.youdianda.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public class PageUtil<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    //总数
    private Long count;
    //当前页面
    private Long currentPage;
    //列表数据
    private List<T> data;
    //每页条数
    private Long pageSize;
    //总页数
    private Long totalPages;

    public static <T> PageUtil<T> pageUtil(Page<T> pageInfo){

        PageUtil<T> pageUtil = new PageUtil<>();

        //总数据条数,取自Page.getrecords().size()
        pageUtil.count = pageInfo.getTotal();

        //目前页码,取自Page.getCurrent()
        pageUtil.currentPage = pageInfo.getCurrent();

        //每页条数,取自Page.getSize()
        pageUtil.pageSize = pageInfo.getSize();

        //总页数,通过计算得出
        pageUtil.totalPages = (pageInfo.getTotal()/pageInfo.getSize())+1;

        //数据列表,取自Page.getRecords
        pageUtil.data = pageInfo.getRecords();

        return pageUtil;
    }
}
